<?php
class picturesdao extends daobase {
	public function getPicturesFolder($ownerId){
		$sql = "select * from picturesfolder where ownerId = '$ownerId' order by createDate desc, updateDate desc";
		$folders = $this->fetchit($sql);
		return $folders;
	}
	public function countPictures($folderId){
		$sql = "select count(id) from pictures where folderId = '$folderId'";
		$number = $this->fetchit($sql);
		return $number[0][0];
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $name
	 * @param unknown_type $des
	 * @param unknown_type $quanxian
	 * @param unknown_type $pwd
	 * @param unknown_type $ownerId
	 * @param unknown_type $pwd
	 * @return ture or false
	 */
	public function createPicFolder($name,$des,$quanxian,$pwd,$ownerId,$kind,$createDate){
		$sql = "insert into picturesfolder(name,description,quanXian,pwd,ownerId,kind,createDate) 
				values('$name','$des','$quanxian','$pwd','$ownerId','$kind','$createDate')";
		$create = $this->queryit($sql);
		if($create){
			$sql = "select * from picturesfolder where name='$name' and ownerId='$ownerId' and createDate='$createDate'";
			$newCreate =$this->fetchit($sql);
			return $newCreate[0];
		}
		return $create;
	}
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $name
	 * @param unknown_type $path
	 * @param unknown_type $folderId
	 * @return unknown
	 */
	public function addPicture($name,$path,$folderId){
		$sql = "delete from pictures where name='$name' and folderId = '$folderId'";
		$this->queryit($sql);
		$sql = "insert into pictures(name,path,folderId) values('$name','$path','$folderId')";
		$add = $this->queryit($sql);
		return $add;
	}
	
	public function getPicS($foderId){
		$sql = "select * from pictures where folderId ='$foderId' order by id desc";
		$pics = $this->fetchit($sql);
		return $pics;
	}
	
	public function getTargetPicInfo($picId,$folderId){
		$targetPicInfo = array();
		$sql = "select * from pictures where id = '$picId'";
		$targetPic = $this->fetchit($sql);
		$targetPic = $targetPic[0];
		$sql = "select count(id) from pictures where id < '$picId' and folderId ='$folderId' ";
		$number  = $this->fetchit($sql);
		$number =$number[0][0]-1;
		if ($number<0){
			$number=0;
		}
		$sql = "select id from pictures where id < '$picId' and folderId ='$folderId' limit $number,1";
		$nextPic = $this->fetchit($sql);
		$nextPic = $nextPic[0][0];
		$sql = "select id from pictures where id > '$picId' and folderId ='$folderId' limit 0,1";
		$prePic = $this->fetchit($sql);
		$prePic = $prePic[0][0];
		$targetPicInfo['target'] = $targetPic;
		$targetPicInfo['pre'] = $prePic;
		$targetPicInfo['next'] = $nextPic;
		return $targetPicInfo;
	}
	public function getPictureFolderCover($folderId){
		$sql = "select cover from picturesfolder where id = '$folderId'";
		$cover = $this->fetchit($sql);
		return $cover[0][0];
		
	}
	
	public function setPictureFolderCover($folderId,$cover){
		$sql = "update picturesfolder set cover = '$cover' where id = '$folderId'";
		$set = $this->queryit($sql);
		return $set;
		
	}
	
	public function deleteTargetPic($picId){
		$parth = $this->getTargetPicPath($picId);
		$sql = "select id from picturesfolder where cover = '$parth'";
		$cover  = $this->fetchit($sql);
		$cover = $cover[0][0];
		if ($cover){
			$sql  = "update picturesfolder set cover='' where id='$cover'";
			$this->queryit($sql);
		}
		$sql = "delete from pictures where id = '$picId'";
		$delete = $this->queryit($sql);
		return $delete;
	}
	
	public function getTargetPicPath($id){
		$sql = "select path from pictures where id='$id'";
		$parth = $this->fetchit($sql);
		return $parth[0][0];
	}
	
	public function deletePicFolder($folderId){
		$sql ="delete from picturesfolder where id='$folderId'";
		$delete = $this->queryit($sql);
		return $delete;
	}

}